<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Edit a profile.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Inherited from Editaction.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_editaction.php';

require_once INSTALLDIR . '/widgets/slh_form_editprofileuser.php';

require_once INSTALLDIR . '/classes/User.php';
require_once INSTALLDIR . '/classes/Profile.php';


class EditprofileuserAction extends EditAction
{
	/**
	 * User and Profile. 
	 */
	var $user = null;
	var $profile = null;
	
	function title()
	{
		return '编辑社联会用户!';
	}
	
	function prepare($args)
	{
		if ( !Rights::hasRight(slh_get_userrights(), RIGHT_PROFILE)) {
			return false;
		}
		
		parent::prepare($args);
		
		// Parameter from 'get' url
		$guser_id = $this->getTrimmedArg('guser_id');
		
		if ( !is_numeric($guser_id)) {
			return false;
		}
		
		// Check the source requiring this page
		$formtoken = $this->getTrimmedArg('formtoken');
		
		if (empty($formtoken)) {
			// Just 'get' the content.
			$this->user = User::staticGet('id', $guser_id);
			$this->profile = Profile::staticGet('user_id', $guser_id);
			
			if (empty($this->profile)) {
				$this->clientError('Cannot find the profile.');
			} else if (empty($this->user)) {
				$this->clientError('Cannot find the user.');
			}
		} else if ('editprofile' == $formtoken) {
			// Edit post.
		}
		
		return true;
	}
	
	function handle($args)
	{
		parent::handle($args);
		
		if ($this->user && $this->profile) {
			$this->args['username'] = $this->user->username;
			$this->args['realname'] = $this->profile->realname;
			$this->args['pinyin'] = $this->profile->pinyin;
			$this->args['department'] = $this->profile->department;
			
			$this->showAction();
		} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$this->tryEdit();
		}
	}
	
	/**
	 * Shows the guidance.
	 * Overriding.
	 */
	function showGuidance()
	{
		// Html code here.
		$this->element('h3', null, '编辑一个社联会用户');
		
		$this->instruction('呃,说明下编辑的规则,怎么编辑好了.');
		
		// If added successfully, then?
		if ($this->edited) {
			$this->element('p', 'cls_p_success', '编辑成功!');
			// Or show a nav.
		}
	}
	
	/**
	 * Shows the widgets like form, or etc.
	 * Overriding.
	 */
	function showForm()
	{
		$form = new EditprofileuserForm($this, $this->args);
		$form->show();
	}
	
	/**
	 * Try to edit the profile of the database.
	 */
	function tryEdit()
	{
		// CSRF protection and Refreshing-post protection.
		$this->checkSessionToken();
		
		// For normal editing.
		$user_id	= $this->getTrimmedArg('user_id');
		$username	= $this->getTrimmedArg('username');
		$realname	= $this->getTrimmedArg('realname');
		$pinyin		= $this->getTrimmedArg('pinyin');
		$department	= $this->getTrimmedArg('department');
		
// TODO: Validates the data.
		if (empty($username)) {
			$this->showAction('请输入用户名.');
		} else if (empty($realname)) {
			$this->showAction('请输入真实姓名.');
		} else if (empty($department)) {
			$this->showAction('请输入所属部门信息.');
		} else {
			// Update the profile.
			if (empty($user_id)) {
				$this->clientError('Cannot find the identity of profile.');
			}
			
			$this->user = User::staticGet('id', $user_id);
			$this->profile = Profile::staticGet('user_id', $user_id);
			
			if (empty($this->profile)) {
				$this->clientError('Cannot find the profile.');
			} else if (empty($this->user)) {
				$this->clientError('Cannot find the user.');
			}
			
			$origUser = clone($this->user);
			$origProfile = clone($this->profile);
			
			// Readonly!
			// $this->user->username	= $username;
			// $this->profile->realname	= $realname;
			$this->profile->pinyin		= $pinyin;
			$this->profile->department	= $department;
			
			$this->user->query('BEGIN');
			
			$u = $this->user->update($origUser);
			$p = $this->profile->update($origProfile);
			
			if ( !$u || !$p) {
				// FIXME: Logs.
				$user->query('ROLLBACK');
				$this->showAction('编辑用户失败!');
				return;
			}
			
			$this->user->query('COMMIT');
			
			// Unset the seesion token preventing post-again.
			$this->unsetSessionToken();
			
			$this->edited = true;
			$this->showAction();
		}
	}
	
}

?>